package com.wms.presenters;

import java.util.ArrayList;

import com.gatech.wms.R;
import com.wms.assets.Item;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

/**
 * Loose presenter for main screen AFTER login.
 * TODO: The layout contains a lot of duplicate code with login_screen. Consider restructuring.
 * @author Nathan
 *
 */
public class MainScreenActivity extends WMSActivity {
	/**
	 * Initializes this activity by setting the layout.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen);
		Intent i = getIntent();
		Bundle b = i.getExtras();
		if (b == null || !b.getBoolean("admin")) {
			Button adm = (Button)findViewById(R.id.btnAdmin);
			adm.setVisibility(View.INVISIBLE);
		}
		setupButtons();
	}
	
	/**
	 * sets onClickListeners for all the buttons
	 */
	private void setupButtons() {
		Button lostSomething = (Button)findViewById(R.id.button1);
		Button administrate = (Button)findViewById(R.id.btnAdmin);
		Button foundSomething = (Button)findViewById(R.id.button2);
		Button searchSomething = (Button)findViewById(R.id.button3);
		
		lostSomething.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final boolean auth = getUserModel().getAuthenticated();
				if (auth) {
					Intent i = new Intent(MainScreenActivity.this, LostItemListActivity.class);
					ArrayList<Item> items = new ArrayList<Item>();
	        		i.putExtra("items", items);
	        		startActivityForResult(i, 0);
				} else {
					Toast.makeText(MainScreenActivity.this, "Log in First!", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		administrate.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final boolean auth = getUserModel().getAuthenticated();
				if (auth) {
					Intent i = new Intent(MainScreenActivity.this, AdminActivity.class);
	        		startActivityForResult(i, 0);
				} else {
					Toast.makeText(MainScreenActivity.this, "Log in First!", Toast.LENGTH_LONG).show();
				}
			}
		});

		foundSomething.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final boolean auth = getUserModel().getAuthenticated();
				if (auth) {
					Intent j = new Intent(MainScreenActivity.this, FoundItemListActivity.class);
					startActivityForResult(j, 0);
				}
				else {
					Toast.makeText(MainScreenActivity.this, "Log in First!", Toast.LENGTH_LONG).show();
				}
			}
		});
		
		searchSomething.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				final boolean auth = getUserModel().getAuthenticated();
				if (auth) {
					Intent k = new Intent(MainScreenActivity.this, SearchScreenActivity.class);
					startActivityForResult(k, 0);	
				} else {
					Toast.makeText(MainScreenActivity.this, "Log in First!", Toast.LENGTH_LONG).show();
				}
			}
		});
	}
	
	@Override
	protected void onActivityResult(int code, int result, Intent data) {
		super.onActivityResult(code, result, data);
		if (result == 1) {
			Button adm = (Button)findViewById(R.id.btnAdmin);
			adm.setVisibility(View.VISIBLE);
		}
	}
}
